/**
 * @Author: XXHH
 * @CreateTime: 2024-12-23
 */
public class Demo33 {
    // 有三个线程，分别只能打印A，B和C
    //  要求按顺序打印ABC，打印10次
    public static void main(String[] args) throws InterruptedException {
        Object locker1 = new Object();
        Object locker2 = new Object();
        Object locker3 = new Object();
        Thread t1 = new Thread(()->{
            try {
                for(int i = 0;i<10;i++){
                    synchronized (locker1) {
                        locker1.wait();
                    }
                    System.out.print('A');
                    synchronized (locker2){
                        locker2.notify();
                    }
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        Thread t2 = new Thread(()->{
            try {
                for(int i = 0;i<10;i++){
                    synchronized (locker2) {
                        locker2.wait();
                    }
                    System.out.print('B');
                    synchronized (locker3){
                        locker3.notify();
                    }
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        Thread t3 = new Thread(()->{
            try {
                for(int i = 0;i<10;i++){
                    synchronized (locker3) {
                        locker3.wait();
                    }
                    System.out.println('C');
                    synchronized (locker1){
                        locker1.notify();
                    }
                }

            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        t1.start();
        t2.start();
        t3.start();

        // 为了 让 t1,t2,t3 三个线程完全加载完毕，形成一个闭环
        Thread.sleep(1000);
        synchronized (locker1){
            locker1.notify();
        }
    }
}
